package com.zhao.integration.jms;

import javax.jms.Connection;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.stereotype.Component;

@Component
public class MessageReceiver extends Messager {

	public void receive(String queueName) throws Exception {
		Connection connection = connection(connectionFactory());
		Session session = session(connection);
		MessageConsumer messageConsumer = messageConsumer(session, destination(session, queueName));
		try {
			while (true) {
				//设置接收者接收消息的时间，为了便于测试，这里谁定为100s
				TextMessage message = (TextMessage) messageConsumer.receive(100000);
				if (null != message) {
					System.out.println("收到消息" + message.getText());
				} else {
					break;
				}
			}
		} finally {
			close(connection);
		}
	}
}